﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Models;
using BLL;
using System.Security.Cryptography;
using System.Web.Security;

namespace _2302MVCDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        [Authorize]//特性  要求授权才能进入该方法
        public ActionResult Index()
        {
            List<Customer> list = CustomerManager.GetCustomers();
            ViewBag.clist = list;
            return View();
        }

        public ActionResult LogOut() 
        {
            FormsAuthentication.SignOut();//清除授权
            return RedirectToAction("Index", "Login");
        }

        public ActionResult CustomerList()
        {
            List<Customer> list = CustomerManager.GetCustomers();
            ViewBag.clist = list;
            return View(list);
        }

        public ActionResult Del(string cid)
        {
            //删除
            if (!CustomerManager.DeleteCustomerByID(cid))
            {
                Response.Write("<script>alert('删除失败！');</script>");
            }
            //刷新
            List<Customer> list = CustomerManager.GetCustomers();
            ViewBag.clist = list;
            return View("CustomerList", list);
        }

        public ActionResult Add(Customer c)
        {
            //添加
            if (!CustomerManager.AddCustomer(c))
            {
                Response.Write("<script>alert('添加失败！');</script>");
            }
            //刷新
            List<Customer> list = CustomerManager.GetCustomers();
            ViewBag.clist = list;
            return View("CustomerList", list);
        }

        public ActionResult Search(string name)
        {
            List<Customer> list = CustomerManager.SearchCustomersByName(name);
            ViewBag.clist = list;
            return View("CustomerList", list);
        }

        public ActionResult Edit(string cid)
        {
            Customer c = CustomerManager.SearchCustomersByID(cid);
            return View(c);
        }

        public ActionResult Save(Customer c)
        {
            if (!CustomerManager.UpdateCustomer(c))
            {
                Response.Write("<script>alert('保存失败！');</script>");
            }
            //返回编辑页面
            //Customer u = CustomerManager.SearchCustomersByID(c.CustomerID);
            //return View("Edit", u);
            //返回客户列表页面
            List<Customer> list = CustomerManager.GetCustomers();
            ViewBag.clist = list;
            return View("CustomerList", list);
        }
    }
}